package com.exam.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.exam.dao.QuestionDbDao;
import com.exam.model.QuestionDbModel;
import com.exam.service.PageService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class QuestionDBAction  extends ActionSupport implements RequestAware,
ModelDriven<QuestionDbModel>,Preparable,ServletResponseAware{
	private static final long serialVersionUID = -920194646704816029L;
	private HttpServletResponse response;
	private PrintWriter out = null;
	private QuestionDbDao qdao=null;
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	private String pageNow;  //分页
	public String getPageNow() {
		return pageNow;
	}
	public void setPageNow(String pageNow) {
		this.pageNow = pageNow;
	}
	private QuestionDbModel qdm;
	private String id;	    //题库id
	private int status;  //状态
	
	public String list(){
		qdao = new QuestionDbDao();
		try {
			PageService page = new PageService(qdao.getCount(),"db-list.action?",pageNow);
			this.request.put("QuestionDBList", qdao.getListByLimit(page.getLimit()));
			this.request.put("pageInfo", page.getPageInfo());  //分页信息
			this.request.put("pageList", page.getPageList());  //分页按钮
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list-success";
	}
	
	/**
	 * 添加个题库
	 * @return
	 */
	public String save(){
		try {
			out = response.getWriter();
			if(new QuestionDbDao().addOneQuestionDB(qdm)){
				out.write("success");
			}else{
				out.write("error");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void prepareSave(){
		this.qdm = new QuestionDbModel();
	}
	
	public void validateSave(){
		
		if(qdm.getDb_name().length() ==0 || qdm.getDb_name().length()>50){
			addFieldError("db_name", "题库名称长度在1-50个字符之间!");
		}
		if(qdm.getDb_info().length()>200){
			addFieldError("db_info", "题库描述不能大于200位!");
		}
	}
	
	
	/*
	 * 更新题库
	 */
	public String update(){
		try {
			out = response.getWriter();
			if(new QuestionDbDao().updateOneQDB(qdm)){
				out.print("success");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void prepareUpdate(){
		qdm = new QuestionDbModel();
	}
	
	/*
	 * 编辑题库
	 */
	public String edit(){
		return "edit";
	}
	public void prepareEdit(){
		 try {
			qdm = new QuestionDbDao().getOneQDB(id);
			System.out.println(qdm.getDb_name());
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}
	
	/**
	 * 删除题库
	 * @return
	 */
	public String delete(){
		new QuestionDbDao().deleteOneQDB(id);
		System.out.println("delete....");
		return "delete-success";
	}
	
	public String changeStatus(){
		new QuestionDbDao().changeStatus(id, status);
		return "status";
	}
	
	Map<String, Object> request = null;
	@Override
	public void setRequest(Map<String, Object> req) {
		this.request = req;
	}

	@Override
	public QuestionDbModel getModel() {
		return qdm;
	}

	@Override
	public void prepare() throws Exception {
		System.out.println("execute...");
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

}
